<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="title" :visible="isShowDialog" @close="onCancel">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="deptForm" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择" @focus="getEmployeeSimple">
          <el-option v-for="item in peoples" :key="item.id" :value="item.username" :label="item.password" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="onsubmit">{{ showTitle }}</el-button>
        <el-button size="small" @click="onCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
// 获取所有部门的api
import { getDepartments } from '@/api/departments'
// 获取员工列表的api
import { getEmployeeSimple } from '@/api/employees'
// 添加新部门，获取部门详情
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'

export default {
  name: 'AddDept',

  props: {
    isShowDialog: {
      type: Boolean,
      default: true
    },

    title: {
      type: String,
      required: true
    },

    // 子组件tree-tools->父组件->子组件dialog，当前操作的节点
    treeNode: {
      type: Object,
      default: null
    }

  },

  data() {
    // 自定义校验规则，判断公司部门名称和编号是否重复
    const checkNameRepeat = async(rule, value, callback) => {
      // 获取改组织的所有部门
      const { depts } = await getDepartments()

      // 判断当前操作节点的子节点与要添加的名称是否重复
      // 判断是修改还是新增
      let isRepeat = false
      if (this.formData.id) {
        isRepeat = depts.filter(item => item.pid === this.treeNode.id && item.id !== this.formData.id)
          .some(item => item.name === value)
      } else {
        isRepeat = depts.filter(item => item.pid === this.treeNode.id).some(item => item.name === value)
      }

      isRepeat ? callback(new Error(`已经存在与${value}名称相同的部门了`)) : callback()
    }

    const checkCodeRepeat = async(rule, value, callback) => {
      // 获取改组织的所有部门
      const { depts } = await getDepartments()

      // 判断当前操作节点的子节点与要添加的名称是否重复
      let isRepeat = false
      if (this.formData.id) {
        isRepeat = depts.some(item => item.id !== this.formData.id && item.code === value && value)
      } else {
        isRepeat = depts.some(item => item.code === value && value)
      }

      isRepeat ? callback(new Error(`已经存在与${value}编号相同的部门了`)) : callback()
    }

    return {
      formData: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },

      // 员工列表
      peoples: [],

      rules: {
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: checkNameRepeat }],
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: checkCodeRepeat }],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }]
      }
    }
  },

  computed: {
    showTitle() {
      return this.formData.id ? '修改' : '确定'
    }
  },

  mounted() {

  },

  methods: {

    // 获取员工列表
    async getEmployeeSimple() {
      this.peoples = await getEmployeeSimple()
    },

    // 添加部门
    onsubmit() {
      this.$refs.deptForm.validate(async isOk => {
        if (isOk) {
          // 判断添加部门还是修改部门
          if (this.formData.id) {
            await updateDepartments(this.formData)
            this.$message.success('部门修改成功')
          } else {
            await addDepartments({ ...this.formData, pid: this.treeNode.id })
            this.$message.success('部门新增成功')
          }

          // 通知父组件重新拉取
          this.$emit('addDepts')
        }

        this.formData = {
          name: '',
          code: '',
          manager: '',
          introduce: ''
        }

        // this.$emit('close')
        // 使用sync修饰符，可以在子组件中直接修改props的值
        this.$emit('update:isShowDialog', false)

        this.$refs.deptForm.resetFields()
      })
    },

    // 取消添加
    onCancel() {
      // this.$refs.deptForm.resetFields()
      this.formData = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$emit('update:isShowDialog', false)

      this.$refs.deptForm.resetFields()
    },

    // 获取部门详情
    async getDepartDetail(id) {
      this.formData = await getDepartDetail(id)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
